package com.computergames.server.server.handler;

import com.computergames.server.domain.ClientState;
import com.computergames.server.message.ErrorResponseMessage;
import com.computergames.server.message.StageSelectionRequestMessage;
import com.computergames.server.message.StageSelectionResponseMessage;
import com.computergames.server.service.Interface.CheckAndUpdateClientState;
import com.computergames.server.util.SpringUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;

@ChannelHandler.Sharable
public class StageSelectionRequestMessageHandler extends SimpleChannelInboundHandler<StageSelectionRequestMessage> {
    CreateRoomRequestMessageHandler CREATE_ROOM_HANDLER = new CreateRoomRequestMessageHandler();
    JoinRoomRequestMessageHandler JOIN_ROOM_HANDLER = new JoinRoomRequestMessageHandler();
    QuitRoomRequestMessageHandler QUIT_ROOM_HANDLER = new QuitRoomRequestMessageHandler();
    MoveRequestMessageHandler MOVE_HANDLER = new MoveRequestMessageHandler();
    ReadyRequestMessageHandler READY_HANDLER = new ReadyRequestMessageHandler();
    AcknowledgeMessageHandler ACKNOWLEDGE_HANDLER = new AcknowledgeMessageHandler();
    BidRequestMessageHandler BID_HANDLER = new BidRequestMessageHandler();
    QuitHandler QUIT_HANDLER = new QuitHandler();

    private static final AttributeKey<String> USER_STAGE_KEY = AttributeKey.valueOf("USER_STAGE");

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, StageSelectionRequestMessage msg) throws Exception {
        int stage = msg.getStage();
        CheckAndUpdateClientState checkAndUpdateClientState = SpringUtil.getBean(CheckAndUpdateClientState.class);
        ChannelPipeline pipeline = ctx.pipeline();

        if(checkAndUpdateClientState.checkState(ctx, ClientState.MODE_SELECTED)){
            if(stage == 1){
                checkAndUpdateClientState.updateState(ctx.channel(), ClientState.STAGE_SELECTED);
                ctx.channel().attr(USER_STAGE_KEY).set("小组赛");
                pipeline.addLast(CREATE_ROOM_HANDLER);
                pipeline.addLast(JOIN_ROOM_HANDLER);
                pipeline.addLast(READY_HANDLER);
                pipeline.addLast(ACKNOWLEDGE_HANDLER);
                pipeline.addLast(BID_HANDLER);
                pipeline.addLast(QUIT_ROOM_HANDLER);
                pipeline.addLast(MOVE_HANDLER);
                pipeline.addLast(QUIT_HANDLER);
                pipeline.addLast(new MatchingRequestMessageHandler());
                ctx.writeAndFlush(new StageSelectionResponseMessage(true,
                        """
                                =============================================
                                \
                                create [room name] [room type] [member1,member2][可选]
                                \
                                join [room name]
                                \
                                members [room name]
                                \
                                remove [member1,member2]
                                \
                                quit
                                \
                                ready [true/false]
                                \
                                match
                                \
                                shutdown
                                \
                                =============================================
                                \s"""));
                //                                send [qizi] [coordinate,coordinate] [棋类]
                //                                send [card,card...] [牌类]
                //
                //                                acknowledge [应答消息]
            }
            else if(stage == 2){
                checkAndUpdateClientState.updateState(ctx.channel(), ClientState.STAGE_SELECTED);
                ctx.channel().attr(USER_STAGE_KEY).set("复赛");
                pipeline.addLast(CREATE_ROOM_HANDLER);
                pipeline.addLast(JOIN_ROOM_HANDLER);
                pipeline.addLast(READY_HANDLER);
                pipeline.addLast(ACKNOWLEDGE_HANDLER);
                pipeline.addLast(BID_HANDLER);
                pipeline.addLast(QUIT_ROOM_HANDLER);
                pipeline.addLast(MOVE_HANDLER);
                pipeline.addLast(QUIT_HANDLER);
                pipeline.addLast(new MatchingRequestMessageHandler());
                ctx.writeAndFlush(new StageSelectionResponseMessage(true,
                        """
                                =============================================
                                \
                                create [room name] [room type] [m1,m2][可选]
                                \
                                members [room name]
                                \
                                join [room name]
                                \
                                send [qizi] [coordinate,coordinate] [half round] [round] [棋类]
                                send [card,card...] [牌类]
                                \
                                ready [true/false]
                                \
                                quit
                                \
                                shutdown
                                \
                                =============================================
                                \s"""));
            }
            else if(stage == 3){
                checkAndUpdateClientState.updateState(ctx.channel(), ClientState.STAGE_SELECTED);
                ctx.channel().attr(USER_STAGE_KEY).set("决赛");
                pipeline.addLast(CREATE_ROOM_HANDLER);
                pipeline.addLast(JOIN_ROOM_HANDLER);
                pipeline.addLast(READY_HANDLER);
                pipeline.addLast(ACKNOWLEDGE_HANDLER);
                pipeline.addLast(BID_HANDLER);
                pipeline.addLast(QUIT_ROOM_HANDLER);
                pipeline.addLast(MOVE_HANDLER);
                pipeline.addLast(QUIT_HANDLER);
                pipeline.addLast(new MatchingRequestMessageHandler());
                ctx.writeAndFlush(new StageSelectionResponseMessage(true,
                        """
                                =============================================
                                \
                                create [room name] [room type] [m1,m2][可选]
                                \
                                members [room name]
                                \
                                join [room name]
                                \
                                send [qizi] [coordinate,coordinate] [half round] [round] [棋类]
                                send [card,card...] [牌类]
                                \
                                ready [true/false]
                                \
                                quit
                                \
                                shutdown
                                \
                                =============================================
                                \s"""));
            }
            else{
                ctx.writeAndFlush(new ErrorResponseMessage("输入错误，请重新输入"));
            }
        }

    }
}
